package repository;

import java.util.List;

import org.hibernate.Session;

import model.Chamado;
import model.ContatoBeijamim;
import utility.DAO;
import utility.HibernateUtil;

public class ChamadoRepository implements DAO<Chamado>{

	private Session session;
	
	@Override
	public void cadastrar(Chamado objeto) {

		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		session.save(objeto); 
		session.getTransaction().commit();
		
	}

	@Override
	public List<Chamado> listar() {

		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		List l1 = session.createQuery("from tb_chamados WHERE chamado_aberto = 1 and cancelado = 0 order by data_abertura").list(); 
		session.getTransaction().commit(); 
		
		return l1;
	}

	@Override
	public void alterar(Chamado objeto) {
		
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		session.update(objeto); 
		session.getTransaction().commit();
		
	}

	@Override
	public void remover(Chamado objeto) {
		
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		session.delete(objeto); 
		session.getTransaction().commit();
		
	}

	@Override
	public Chamado buscar(Long id) {
		
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		Chamado chamado = (Chamado)session.get(Chamado.class, id); //
		session.getTransaction().commit(); 
		
		return chamado;
	}

}
